package com.ruoyi.web.controller.common;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.web.domain.*;
import com.ruoyi.web.service.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Component("sitemap")
public class sitemap {

    @Resource
    private IQuestionService questionService;
    @Resource
    private IDrugService drugService;
    @Resource
    private IOldNewsService oldNewsService;
    @Resource
    private IOldQuestionService oldquestionService;
    @Resource
    private INewsService newsService;

    /*
     * 定时更新sitemap
     * */
    public void renewSiteMap(){

        /*PC新闻详情url*/
        String pcxwxqUrl = "https://www.1blv.cn/xwxq/";
        /*PC旧新闻详情Url*/
        String oldpcxwxqUrl = "https://www.1blv.cn/newsdetail/";
        HashSet<String> pcxwxqSet = new HashSet();

        /*M端新闻详情Url*/
        String mxwxqUrl = "https://m.1blv.cn/xwxq/";
        /*M端旧新闻详情Url*/
        String oldmxwxqUrl = "https://m.1blv.cn/newsdetail/";
        HashSet<String> mxwxqSet = new HashSet();

        /*PC问答详情url*/
        String pcwdxqUrl = "http://www.1blv.cn/wdxq/";
        /*PC旧问答详情*/
        String oldpcwdxqUrl = "https://www.1blv.cn/quesdetail/";
        HashSet<String> pcwdxqSet = new HashSet();

        /*M端问答详情url*/
        String mwdxqUrl = "https://m.1blv.cn/wdxq/";
        /*M端旧问答详情*/
        String oldmwdxqUrl = "https://m.1blv.cn/quesdetail/";
        HashSet<String> mwdxqSet = new HashSet();

        /*PC药品详情Url*/
        String pcypbkUrl = "https://www.1blv.cn/ypbk/";
        /*PC药品价格与购买Url*/
        String pcjgygmUrl = "https://www.1blv.cn/jgygm/";
        /*PC药品咨询Url*/
        String pcypzxUrl = "https://www.1blv.cn/ypzx/";
        /*PC药品问答Url*/
        String pcypwdUrl = "https://www.1blv.cn/ypwd/";
        HashSet<String> pcypbkSet = new HashSet();

        /*M端药品详情Url*/
        String mypbkUrl = "https://m.1blv.cn/ypbk/";
        /*M端药品价格与购买Url*/
        String mjgygmUrl = "https://m.1blv.cn/jgygm/";
        /*M端药品咨询Url*/
        String mypzxUrl = "https://m.1blv.cn/ypzx/";
        /*M端药品问答Url*/
        String mypwdUrl = "https://m.1blv.cn/ypwd/";
        HashSet<String> mypbkSet = new HashSet();

        String PC_URLS_FILE = "/disk/sitemap/PC/sitemap1.txt";

        String M_URLS_FILE = "/disk/sitemap/M/sitemap1.txt";

        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH ) -1, 0, 0, 0);
        Date startTime  = cal.getTime();
        Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        cal2.set(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH), cal2.get(Calendar.DAY_OF_MONTH) -1, 23, 59, 59);
        Date endTime  = cal2.getTime();

        /*PC新闻详情*/
        News news = new News();
        news.setState("1");
        news.setStartTime(startTime);
        news.setEndTime(endTime);

        List<News> newsList = newsService.selectNewsList(news);

        if (ObjectUtil.isNotEmpty(newsList)){
            newsList.forEach(bean -> {
                pcxwxqSet.add(pcxwxqUrl + bean.getId() + ".html");
                mxwxqSet.add(mxwxqUrl + bean.getId() + ".html");
            });
        }

        /*PC旧新闻详情*/
        OldNews oldNews = new OldNews();
        oldNews.setState("1");
        oldNews.setStartTime(startTime);
        oldNews.setEndTime(endTime);
        List<OldNews> oldNewsList = oldNewsService.selectOldNewsList(oldNews);
        if (ObjectUtil.isNotEmpty(oldNewsList)){
            oldNewsList.forEach(bean -> {
                pcxwxqSet.add(oldpcxwxqUrl + bean.getId() + ".html");
                mxwxqSet.add(oldmxwxqUrl + bean.getId() + ".html");
            });
        }

        /*PC旧问答详情*/
        OldQuestion oldQuestion = new OldQuestion();
        oldQuestion.setState("1");
        oldQuestion.setStartTime(startTime);
        oldQuestion.setEndTime(endTime);
        List<OldQuestion> oldQuestionList = oldquestionService.selectQuestionList(oldQuestion);
        if (ObjectUtil.isNotEmpty(oldQuestionList)){
            oldQuestionList.forEach(bean -> {
                pcwdxqSet.add(oldpcwdxqUrl + bean.getId() + ".html");
                mwdxqSet.add(oldmwdxqUrl + bean.getId() + ".html");
            });
        }

        /*PC问答详情*/
        Question question = new Question();
        question.setState("1");
        question.setStartTime(startTime);
        question.setEndTime(endTime);

        List<Question> questionList = questionService.selectQuestionList(question);

        if (ObjectUtil.isNotEmpty(questionList)){
            questionList.forEach(bean -> {
                pcwdxqSet.add(pcwdxqUrl + bean.getId() + ".html");
                mwdxqSet.add(mwdxqUrl + bean.getId() + ".html");
            });
        }

        /*PC药品详情*/
        Drug drug = new Drug();
        drug.setState("1");
        drug.setStartTime(startTime);
        drug.setEndTime(endTime);
        List<Drug> drugList = drugService.selectDrugList(drug);
        if (ObjectUtil.isNotEmpty(drugList)){
            drugList.forEach(bean -> {
                pcypbkSet.add(pcypbkUrl + bean.getId() + ".html");
                pcypbkSet.add(pcjgygmUrl + bean.getId() + "/1.html");
                pcypbkSet.add(pcypzxUrl + bean.getId() + "/1.html");
                pcypbkSet.add(pcypwdUrl + bean.getId() + "/1.html");

                mypbkSet.add(mypbkUrl + bean.getId() + ".html");
                mypbkSet.add(mjgygmUrl + bean.getId() + "/1.html");
                mypbkSet.add(mypzxUrl + bean.getId() + "/1.html");
                mypbkSet.add(mypwdUrl + bean.getId() + "/1.html");
            });
        }

        try (BufferedWriter writer = new BufferedWriter(new FileWriter(PC_URLS_FILE,true))) {
            for (String foundUrl : pcxwxqSet) {
                writer.newLine();
                writer.write(foundUrl);
            }
            for (String foundUrl : pcwdxqSet) {
                writer.newLine();
                writer.write(foundUrl);
            }
            for (String foundUrl : pcypbkSet) {
                writer.newLine();
                writer.write(foundUrl);
            }

            System.out.println("PC URLs 写入 " + PC_URLS_FILE);
        } catch (IOException e) {
            System.out.println("Failed to write found URLs to file: " + e.getMessage());
        }


        try (BufferedWriter writer = new BufferedWriter(new FileWriter(M_URLS_FILE,true))) {
            for (String foundUrl : mxwxqSet) {
                writer.newLine();
                writer.write(foundUrl);
            }
            for (String foundUrl : mwdxqSet) {
                writer.newLine();
                writer.write(foundUrl);
            }
            for (String foundUrl : mypbkSet) {
                writer.newLine();
                writer.write(foundUrl);
            }

            System.out.println("M URLs 写入 " + PC_URLS_FILE);
        } catch (IOException e) {
            System.out.println("Failed to write found URLs to file: " + e.getMessage());
        }

        System.out.println( "定时任务执行于 --> " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

    }
}
